/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.raitcon.hibernate.db;

import com.raitcon.hibernate.bean.Alumno;
import com.raitcon.hibernate.bean.DetalleLibreta;
import com.raitcon.hibernate.bean.DetalleNota;
import com.raitcon.hibernate.bean.HibernateSessionFactory;
import com.raitcon.hibernate.bean.Libreta;
import com.raitcon.hibernate.bean.Nota;
import com.raitcon.hibernate.factory.HibernateUtil;
import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.Transaction;

/**
 *
 * @author Mike
 */
public class LibretaDB {
    
    protected static Logger log = Logger.getLogger(LibretaDB.class);
    private Session session = null;
    
    
    public LibretaDB(){
        this.session = HibernateUtil.getSessionFactory().getCurrentSession();
    }
    
    
    
    
    public String insertOrUpdateNota(Libreta libreta, List<DetalleLibreta> detalleLibreta, 
            Integer idOption, Libreta libretaSave) {
        String result = "";
         System.out.println("metodo insertOrUpdate  libreta");
         session = HibernateSessionFactory.getSession();
         System.out.println("session: " + session);
         System.out.println("Try");
         Transaction ts = session.beginTransaction();
        try {
             
            System.out.println("Transaction");
            if(libretaSave!=null){
                
                System.out.println("Guardando registro libreta id::"+libretaSave.getIdLibreta());
                log.info("idGrado:"+libretaSave.getGrado().getIdGrado());
                log.info("idGrado:"+libretaSave.getAnio().getIdAnio());
                libreta.setIdLibreta(libretaSave.getIdLibreta());
                
                session.update(libreta);   
            
            }
            else{
                System.out.println("Insertando nuevo registro.... ");
                log.info("idGrado:"+libreta.getGrado().getIdGrado());
                log.info("idGrado:"+libreta.getAnio().getIdAnio());
                session.save(libreta);
            }
                
            
           
            for(DetalleLibreta dt :detalleLibreta ){
                dt.setLibreta(libreta);
                
                if(libretaSave!=null){
                  if(dt.getIdDetalleLibreta()!=-1) { 
                     session.update(dt);
                     System.out.println("actualiza detalle id:"+dt.getLibreta());
                  }else{
                     session.save(dt);
                     System.out.println("inserta detalle id:"+dt.getLibreta());
                  }
                }
                else{
                  session.save(dt);
                //dtDB.save(d, session);
                System.out.println("inserta detalle");
                }
             
            }
            ts.commit();
            System.out.println("Se guardó correctamente");
            result = "Se guardó exitosamente el registo nota id:"+libreta.getIdLibreta();
            return result;
        } catch (HibernateException ex1) {
            System.out.println("HibernateException : " + ex1);
            ts.rollback();
            return "";
        } catch (Exception ex2) {
            System.out.println("Exception : " + ex2);
            ts.rollback();
            return "";
        } finally {
            session.close();
        }
    }
    
    
    public Libreta findLibreta( Integer idGrado, Integer idAnio){
        
         log.info("Iniciando consulta de Libreta...");
         Libreta libreta=null;
        try {
            Transaction tx = session.beginTransaction();
            Query q = session.createQuery("from Libreta as libreta  "
                    + "WHERE id_grado="+idGrado+ " AND id_anio="+ idAnio);

            libreta=(Libreta) q.uniqueResult();
             log.info("Consulta de libreta exitosa.");
             return libreta;
        } catch (Exception e) {
            log.error("Error:" + e.getMessage());
            e.printStackTrace();
        } finally {
            session.close();
        }
        return libreta;
    }
    
}
